// pages/masterList/masterList.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    masterList: [],
    receiveUserIdArray: [],
    hours: '0' + 0, // 时
    minute: '0' + 0, // 分
    second: '0' + 0, // 秒
    time: "",
    newSecond: 0,
    newMinute: 0,
    newHour: 0,
    receiving: "",
    newTime: "",
    publishId: "",
    setInterval: "",
    feeOpen: true,
    feeItemIds: [],
    orderList: [],
    projectMes: [],
    isCheck:0,
    refuseReason:""
  },
  // 确认师傅
  confirm: function(e) {
    var that = this;
    var receiveUserId = e.currentTarget.dataset.id;
    var name = e.currentTarget.dataset.name;
    this.setData({
      publishId:parseInt(this.data.publishId)
    })
    wx.showModal({
      title: '提示',
      content: "确认由"+name+"师傅接单",
      success: function(res) {
        if (res.confirm) {
          wx.request({
            url: 'https://www.xiaozheng8.com/serviceOrder/setStatus/v3',
            data: {
              status: 5,
              publishId: that.data.publishId,
              // orderId: 252,
              receiveUserId: receiveUserId
            },
            method: 'post',
            success: function(res) {
              console.log(res);
              wx.redirectTo({
                url: '../servering/servering?userId=' + res.data.data + "&receiveUserId=" + receiveUserId,
              })
            },
            fail: function(res) {},
            complete: function(res) {},
          })
        }
      }
    })
  },
  // 拒绝师傅抢单
  refuse:function(e){
    var that=this;
    var receiveUserId=e.currentTarget.dataset.id; 
    var name = e.currentTarget.dataset.name;
    this.setData({
      publishId: parseInt(this.data.publishId)
    })
    wx.showModal({
      title: '提示',
      content: "确认要拒绝"+name+"师傅",
      success:function(res){
        if(res.confirm){
          wx.request({
            url: 'https://www.xiaozheng8.com/serviceOrder/grabRecord/update',
            data: {
              receiveUserId: receiveUserId,
              publishId: that.data.publishId
            },
            method: 'post',
            success: function (res) {
              console.log(res);
              that.masterList();
            },
            fail: function (res) { },
            complete: function (res) { },
          })
        }
      }
    })
    

  },
  // 取消订单
  grabOrder: function(options) {
    var that = this;
    this.setData({
      publishId: parseInt(this.data.publishId)
    })
    wx.showModal({
      title: "提示",
      content: '是否确认取消订单',
      confirmText: "确定",
      cancelText: "取消",
      success: function(res) {
        if (res.confirm) {
          wx.request({
            url: 'https://www.xiaozheng8.com/serviceOrder/setStatus/v3',
            method: "post",
            data: {
              "isCancel": true,
              publishId: that.data.publishId
            }
          })
          wx.reLaunch({
            url: '../order/order',
          });
          wx.showModal({
            title: "提示",
            content: '订单已取消',
            showCancel: false
          })
        }
      }
    })
  },
  feeInfo: function() {
    this.setData({
      feeOpen: !this.data.feeOpen,
    })
  },

  // 师傅详情
  masterDetail: function(e) {
    var that=this;
    var receiveUserId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../serverDetail/serverDetail?id=' + receiveUserId+"&publishId="+that.data.publishId+"&fromOrder="+true,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  // 获取师傅列表
  masterList:function(){
    var that=this;
    this.setData({
      publishId: parseInt(this.data.publishId)
    })
    wx.request({
      url: 'https://www.xiaozheng8.com/serviceOrder/grabRecord/find/v2',
      data: {
        publishId: that.data.publishId
      },
      method: 'post',
      success: function (res) {
        console.log(res);
        that.setData({
          masterList: res.data.data,
        });
        var masterList = that.data.masterList;
        for (var i = 0; i < that.data.masterList.length; i++) {
          console.log(i);
          if (that.data.masterList[i].grabStatus == -1) {
           that.data.masterList.splice(i, 1);   
          }
        };
        console.log(masterList)
        that.setData({
          masterList: that.data.masterList
        });
       
      },
      fail: function (res) {
        
      },
      complete: function (res) { },
    })
  },
  // 等待时间
  time: function () {


    const that = this;
    var totalTime=15*24*60*60;
    console.log(totalTime);
    var getTime = 0;
    console.log(that.data.publishId + "这是时间上的id2")
    wx.request({
      url: 'https://www.xiaozheng8.com/clock/get',
      method: "post",

      data: {
        publishId: parseInt(that.data.publishId),
        userType: 0
      },

      success: function (res) {
        wx.hideLoading();
        console.log(res)
        console.log(res.data.data);
        that.setData({
          newTime: res.data.data
        })
        getTime =  res.data.data;
        console.log(that.data.newTime + "时间")

        console.log(that.data.newTime + "计时器时间")

        var time = totalTime-that.data.newTime;
        var second = time % 60;
        var minute = parseInt(time / 60);
        var hours = parseInt(parseInt(time / 60) / 60);
        that.setData({
          second: time % 60,
          minute: Math.floor(time / 60),
          hours: Math.floor((time/ 60/ 60) ),
        });
        if (that.data.second < 10) {
          that.setData({
            second: "0" + that.data.second
          })
        }
        if (that.data.hours < 10) {
          that.setData({
            hours: "0" + that.data.hours
          })
        }
        if (that.data.minute < 10) {
          that.setData({
            minute: "0" + that.data.minute
          })
        }
        if (that.data.minute > 59) {
          that.setData({
            minute: Math.floor(time / 60) % 60
          })
        }
        that.setInterval = setInterval(function () {
          time--;
          if(time<=0){
            wx.request({
              url: 'https://www.xiaozheng8.com/serviceOrder/setStatus/v3',
              method: "post",
              data: {
                "isCancel": true,
                publishId: that.data.publishId
              },
              success(res){
                console.log(res);
                wx.navigateBack({
                  delta: 1,
                  success(){
                    wx.showToast({
                      title: '订单已超时，自动取消',
                      icon: "none",
                      duration: 3000
                    })
                  }
                })
              
              }
            })
          }
          if (time % 60 < 10) {
            that.setData({
              second: '0' + time % 60
            })
          } else {
            that.setData({
              second: time % 60
            })
          }
          if (parseInt(time / 60) % 60 < 10) {
            that.setData({
              minute: '0' + Math.floor(time / 60) % 60
            });

            if (Math.floor(time / 60 / 60) < 10) {
              that.setData({
                hours: '0' + Math.floor(time / 60 / 60)
              })
            } else {
              that.setData({
                hours: Math.floor(time / 60 / 60)
              })
            }
          } else {
            that.setData({
              minute: Math.floor(time / 60) % 60,

            });

            if (Math.floor(time / 60 / 60) < 10) {
              that.setData({
                hours: '0' + Math.floor(time / 60 / 60)
              })
            } else {
              that.setData({
                hours: Math.floor(time / 60 / 60)
              })
            }

            console.log(parseInt(time / 60 / 60));
            console.log(that.data.hours);

          }
        }, 1000)
      }
    });
  },
  changeOrder(){
    console.log(this.data.orderList);
    wx.setStorageSync("orderDataList", null);
    wx.setStorageSync("mes", "");
    // 在这个确认选择的客户类型并保存在本地
    var that=this;
    var client="";
    var factorId="";
    console.log(this.data.orderList[0].factorId);
    wx.request({
      url: 'https://www.xiaozheng8.com/service/factor/findAll',
      method: 'post',
      data: {},
      success: function (res) {
        console.log(res);
        for(var i=0;i<res.data.data.length;i++){
          for(var j=0;j<res.data.data[i].factors.length;j++){
            if(that.data.orderList[0].factorId==res.data.data[i].factors[j].factorId){
              console.log(res.data.data[i].factors[j].factorId);
              console.log(res.data.data[i].factors[j].name);
              wx.setStorageSync("factorId", res.data.data[i].factors[j].factorId);
              wx.setStorageSync("factor", res.data.data[i].factors[j].factor);
              wx.setStorageSync("client", res.data.data[i].factors[j].name);
            }
          }
        }
        wx.navigateTo({
          url: '../orderPage/orderPage?change=' + 1 + "&dataList=" + escape(JSON.stringify(that.data.orderList[0])),
        })
      
      }
    })
   
  
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    console.log(options);
    console.log(options.length);
    if (options.receiveUserIdArray==undefined){
      that.setData({
        publishId: wx.getStorageSync("orderId"),
      })
    
    }else{
      var array = JSON.parse(options.receiveUserIdArray);
      that.setData({
        publishId: options.userId,
        receiveUserIdArray: array
      })
    }
    
    console.log(that.data.receiveUserIdArray);
    wx.request({
      url: 'https://www.xiaozheng8.com/serviceOrder/sendFindById',
      method: "post",
      data: {
        publishId: that.data.publishId,

      },
      success: function(res) {
        console.log(res),
          console.log(that.data.publishId + "punlishId");
        that.setData({
          orderList: res.data.data,
          publishId: that.data.publishId,
          projectMes: res.data.data.project,
          isCheck:res.data.data.isCheck,
          refuseReason:res.data.data.refuseReason
        });
        console.log(that.data.orderList);
        that.setData({
          orderList: [that.data.orderList],
          time: res.data.data.createTime
        });
        var fee = res.data.data.feeItemIds;
        console.log(fee);
        that.data.feeItemIds.push(fee.split(" "));
        that.setData({
          feeItemIds: that.data.feeItemIds,
        })
        console.log(that.data.feeItemIds)
      }
    })
    console.log(that.data.publishId + "这是时间上的id")
    wx.request({
      url: 'https://www.xiaozheng8.com/clock/get',
      method: "post",
      data: {
        publishId: parseInt(that.data.publishId),
        userType: 0
      },
      success: function(res) {
        console.log(res)
        console.log(res.data.data);
        that.setData({
          newTime: res.data.data
        })

      }



    });

    console.log(options);

    this.masterList();
    // wx.request({
    //   url: 'https://www.xiaozheng8.com/receiveUser/findAll',
    //   method: "post",
    //   data: {
    //     page: 0,
    //     size: 5
    //   },
    //   success: function(res) {
    //     console.log(res);
    //     var content = that.data.masterList.concat(res.data.data)
    //     that.setData({
    //       masterList: content,
    //     })
    //   }
    // })
    wx.request({
      url: 'https://www.xiaozheng8.com/receiveUser/findByReceiveUserIdList',
      data: {
        receiveUserIdList: that.data.receiveUserIdArray
      },
      method: 'post',
      success: function(res) {
        console.log(res);
        // that.setData({
        //   masterList: res.data.data,
        // })
      },
      fail: function(res) {},
      complete: function(res) {},
    });
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    // this.order();
    this.masterList();
    this.time();
    if(this.data.isCheck==9){
      clearInterval(this.setInterval);
      clearInterval(this.data.getData);
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearInterval(this.setInterval);
    clearInterval(this.data.getData);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var that = this;
    clearInterval(this.setInterval);
    clearInterval(this.data.getData);

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})